package org.csu.jpetstore.web.servlet;

import org.csu.jpetstore.domain.Account;
import org.csu.jpetstore.domain.Cart;
import org.csu.jpetstore.domain.Order;
import org.csu.jpetstore.service.OrderService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class ViewContinueOrderServlet extends HttpServlet {

    private String cardType;
    private String creditCard;
    private String expiryDate;
    private String orderMsg;
    private List<Integer> idList;

    private String ship_address1;
    private String ship_address2;
    private String ship_firstname;
    private String ship_lastname;
    private String ship_city;
    private String ship_state;
    private String ship_zip;
    private String ship_country;

    private static final String VIEW_NEW_ORDER = "/WEB-INF/jsp/order/NewOrder.jsp";
    private static final String VIEW_CONTINUE_ORDER = "/WEB-INF/jsp/order/ContinueOrder.jsp";

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        OrderService orderService = new OrderService();
        Account loginAccount = (Account)session.getAttribute("loginAccount");
        Order order = new Order();
        idList = orderService.getAllOrderId();

        Date date = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = df.format(date);
        session.setAttribute("time", time);

        cardType = request.getParameter("order.cardType");
        creditCard = request.getParameter("order.creditCard");
        expiryDate = request.getParameter("order.expiryDate");

        ship_address1 = request.getParameter("ship.address1");
        ship_address2 = request.getParameter("ship.address2");
        ship_city = request.getParameter("ship.city");
        ship_state = request.getParameter("ship.state");
        ship_zip = request.getParameter("ship.zip");
        ship_country = request.getParameter("ship.country");
        ship_firstname = request.getParameter("ship.firstName");
        ship_lastname = request.getParameter("ship.lastName");

        if(!validation()) {
            request.setAttribute("orderMsg", this.orderMsg);
            request.getRequestDispatcher(VIEW_NEW_ORDER).forward(request, response);
        }
        else {

            int id = (int)(1000+Math.random()*9000);
            while (!idIsValid(id,idList)) {
                id = (int)(1000+Math.random()*9000);
            }

            order.setOrderId(id);
            order.setUsername(loginAccount.getUsername());
            order.setOrderDate(date);
            order.setShipAddress1(request.getParameter("ship.address1"));
            order.setShipAddress2(request.getParameter("ship.address2"));
            order.setShipCity(request.getParameter("ship.city"));
            order.setShipState(request.getParameter("ship.state"));
            order.setShipZip(request.getParameter("ship.zip"));
            order.setShipCountry(request.getParameter("ship.country"));
            order.setBillAddress1(loginAccount.getAddress1());
            order.setBillAddress2(loginAccount.getAddress2());
            order.setBillCity(loginAccount.getCity());
            order.setBillState(loginAccount.getState());
            order.setBillZip(loginAccount.getZip());
            order.setBillCountry(loginAccount.getCountry());
            order.setCourier("YUNDA");
            order.setShipToFirstName(request.getParameter("ship.firstName"));
            order.setShipToLastName(request.getParameter("ship.lastName"));
            order.setBillToFirstName(loginAccount.getFirstName());
            order.setBillToLastName(loginAccount.getLastName());
            order.setCardType(cardType);
            order.setCreditCard(creditCard);
            order.setExpiryDate(expiryDate);
            order.initOrder((Cart)session.getAttribute("cart"));

            orderService.addOrder(order);
            session.setAttribute("order", order);

            request.getRequestDispatcher(VIEW_CONTINUE_ORDER).forward(request, response);
        }
    }

    private boolean validation() {
        if(creditCard==null || creditCard.equals("")) {
            this.orderMsg = "请输入卡号";
            return false;
        }
        if(expiryDate==null || expiryDate.equals("")) {
            this.orderMsg = "请输入配送日期";
            return false;
        }
        if(ship_address1 == null || ship_address1.equals("")) {
            this.orderMsg = "请输入收货地址1";
            return false;
        }
        if(ship_address2 == null || ship_address2.equals("")) {
            this.orderMsg = "请输入收货地址2";
            return false;
        }
        if(ship_firstname == null || ship_firstname.equals("")) {
            this.orderMsg = "请输入收货人名";
            return false;
        }
        if(ship_lastname == null || ship_lastname.equals("")) {
            this.orderMsg = "请输入收货人姓";
            return false;
        }
        if(ship_city == null || ship_city.equals("")) {
            this.orderMsg = "请输入收货城市";
            return false;
        }
        if(ship_state == null || ship_state.equals("")) {
            this.orderMsg = "请输入收货州";
            return false;
        }
        if(ship_zip == null || ship_zip.equals("")) {
            this.orderMsg = "请输入收货邮政编码";
            return false;
        }
        if(ship_country == null || ship_country.equals("")) {
            this.orderMsg = "请输入收货国家";
            return false;
        }
        return true;
    }

    private boolean idIsValid(int id,List<Integer> list) {
        for(int i=0;i<list.size();i++) {
            if(id == list.get(i))
                return false;
        }
        return true;
    }

}
